package com.example.kami.test;


import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;


import java.util.LinkedHashMap;


/**
 * @author liuhang
 * @description 泡椒云卡密系统 (v1:不需要卡密，v2:需要卡密)
 * @date 2022/02/09
 */
@RestController
@RequestMapping("/v1")
public class PjyKamiController {
    Logger log = LoggerFactory.getLogger(PjyKamiController.class);

    @RequestMapping(value = "/pjy/login")
    public String login(@RequestParam("key") String key) {
        if ("yFMSOAjtoLPX04VE2WjcSOnMP63SUz6C".equals(key)) {
            return "";
        }
        LinkedHashMap map = new LinkedHashMap();
        LinkedHashMap map1 = new LinkedHashMap();
        map1.put("card_type", "季卡");
        map1.put("token", "Ie4tMJRQ9i14t6sjQbBe");
        map1.put("expires", "2029-10-20 09:27:18");
        map1.put("expires_ts", "1999999999");
        map1.put("config", "");

        long time = System.currentTimeMillis() / 1000;
        map1.put("server_time", time);

        map.put("code", 0);
        map.put("message", "ok");
        map.put("result", map1);
        String nonce = getNonce();

        map.put("nonce", nonce);

        String data = "0okcard_type=季卡&config=&expires=2029-10-20 09:27:18&expires_ts=1999999999&server_time=" + time + "&token=Ie4tMJRQ9i14t6sjQbBe" + nonce + key;
        map.put("sign", getMd5(data));
        log.info("泡椒云登录获取key:【{}】返回参数成功！", key);
        return JSON.toJSONString(map);
    }


    /**
     * 此接口专供测试用（可随时屏蔽）
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "/pjy/login/cs")
    public String loginCs(@RequestParam("key") String key) {
        LinkedHashMap map = new LinkedHashMap();
        LinkedHashMap map1 = new LinkedHashMap();
        map1.put("card_type", "季卡");
        map1.put("token", "Ie4tMJRQ9i14t6sjQbBe");
        map1.put("expires", "2029-10-20 09:27:18");
        map1.put("expires_ts", "1999999999");
        map1.put("config", "");

        long time = System.currentTimeMillis() / 1000;
        map1.put("server_time", time);

        map.put("code", 0);
        map.put("message", "ok");
        map.put("result", map1);
        String nonce = getNonce();

        map.put("nonce", nonce);

        String data = "0okcard_type=季卡&config=&expires=2029-10-20 09:27:18&expires_ts=1999999999&server_time=" + time + "&token=Ie4tMJRQ9i14t6sjQbBe" + nonce + key;
        map.put("sign", getMd5(data));
        log.info("泡椒云登录获取key:【{}】返回参数成功！", key);
        return JSON.toJSONString(map);
    }

    /**
     * 此接口专供正式用
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "/pjy/login/zs")
    public String loginZs(@RequestParam("key") String key) {
        LinkedHashMap map = new LinkedHashMap();
        LinkedHashMap map1 = new LinkedHashMap();
        map1.put("card_type", "季卡");
        map1.put("token", "Ie4tMJRQ9i14t6sjQbBe");
        map1.put("expires", "2029-10-20 09:27:18");
        map1.put("expires_ts", "1999999999");
        map1.put("config", "");

        long time = System.currentTimeMillis() / 1000;
        map1.put("server_time", time);

        map.put("code", 0);
        map.put("message", "ok");
        map.put("result", map1);
        String nonce = getNonce();

        map.put("nonce", nonce);

        String data = "0okcard_type=季卡&config=&expires=2029-10-20 09:27:18&expires_ts=1999999999&server_time=" + time + "&token=Ie4tMJRQ9i14t6sjQbBe" + nonce + key;
        map.put("sign", getMd5(data));
        log.info("泡椒云登录获取key:【{}】返回参数成功！", key);
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/pjy/heartbeat")
    public String heartbeat(@RequestParam("key") String key) {
        LinkedHashMap map = new LinkedHashMap();
        LinkedHashMap map1 = new LinkedHashMap();
        map1.put("expires", "2029-10-20 09:27:18");
        map1.put("expires_ts", "1999999999");
        long time = System.currentTimeMillis() / 1000;
        map1.put("server_time", time);

        map.put("code", 0);
        map.put("message", "ok");
        map.put("result", map1);
        String nonce = getNonce();
        map.put("nonce", nonce);

        String data = "0okexpires=2029-10-20 09:27:18&expires_ts=1999999999&server_time=" + time + nonce + key;
        map.put("sign", getMd5(data));
        log.info("泡椒云心跳获取key:【{}】返回参数成功！", key);
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/pjy/notice")
    public String notice(@RequestParam("key") String key) {
        LinkedHashMap map = new LinkedHashMap();
        LinkedHashMap map1 = new LinkedHashMap();
        map1.put("notice", "666");

        map.put("code", 0);
        map.put("message", "ok");
        map.put("result", map1);
        String nonce = getNonce();
        map.put("nonce", nonce);

        String data = "0oknotice=666" + nonce + key;
        map.put("sign", getMd5(data));
        log.info("泡椒云通知获取key:【{}】返回参数成功！", key);
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/pjy/latest_ver")
    public String latest_ver(@RequestParam("key") String key) {
        LinkedHashMap map = new LinkedHashMap();
        LinkedHashMap map1 = new LinkedHashMap();
        map1.put("notice", "666");
        map1.put("url", "");
        map1.put("version", "v4.1.6");
        long time = System.currentTimeMillis() / 1000;
        map1.put("server_time", time);

        map.put("code", 10304);
        map.put("message", "ok");
        map.put("result", map1);
        String nonce = getNonce();
        map.put("nonce", nonce);

        String data = "10304oknotice=666&server_time=" + time + "&url=&version=v4.1.6" + nonce + key;
        map.put("sign", getMd5(data));
        log.info("泡椒云最新版本获取key:【{}】返回参数成功！", key);
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/pjy/call_remote_func")
    public String call_remote(@RequestParam("key") String key) {
        LinkedHashMap map = new LinkedHashMap();
        LinkedHashMap map1 = new LinkedHashMap();
        map1.put("return", "{\"return\":\"gjxkxn\"}");


        map.put("code", 0);
        map.put("message", "ok");
        map.put("result", map1);
        String nonce = getNonce();
        map.put("nonce", nonce);

        String data = "0ok" + nonce + key;
        map.put("sign", getMd5(data));
        log.info("泡椒云最新版本获取key:【{}】返回参数成功！", key);
        return JSON.toJSONString(map);
    }

    private synchronized String getNonce() {
        SnowflakeIdGenerator idWorker = new SnowflakeIdGenerator(0, 0);
        return idWorker.nextId() + "";
    }

    private String getMd5(String str) {
        StringBuffer buffer = new StringBuffer();
        try {
            byte[] digest = java.security.MessageDigest.getInstance("md5").digest(str.getBytes());
            for (int index = 0; index < digest.length; index++) {
                byte b = digest[index];
                int number = b & 0xff;
                String strs = Integer.toHexString(number);
                if (strs.length() == 1) {
                    buffer.append("0");
                }
                buffer.append(strs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }

}
